package com.agricultural.handler.impl;

import com.agricultural.equipment.AgriculturalEquipment;
import com.agricultural.handler.SafetyHandler;
import com.agricultural.strategy.impl.ObstacleAvoidanceStrategy;

// 障碍物检测处理器
public class ObstacleDetectionHandler implements SafetyHandler {
    private SafetyHandler nextHandler; // 下一个处理器
    
    // 设置下一个处理器
    @Override
    public void setNextHandler(SafetyHandler nextHandler) {
        this.nextHandler = nextHandler;
    }
    
    // 处理安全事件
    @Override
    public boolean handleSafetyEvent(AgriculturalEquipment equipment, String eventType, double parameter) {
        if ("obstacle".equals(eventType)) {
            System.out.println("[" + equipment.getType() + "-" + equipment.getId() + "] 障碍物检测处理器接收到事件");
            
            if (parameter < 15.0) { // 如果障碍物距离小于15米
                System.out.println("[" + equipment.getType() + "-" + equipment.getId() + "] 检测到障碍物，距离: " + parameter + "米");
                
                // 切换到避障路径规划策略
                equipment.setPathPlanningStrategy(new ObstacleAvoidanceStrategy());
                
                // 重新规划路径
                java.util.Map<String, Object> environmentData = new java.util.HashMap<>();
                environmentData.put("obstacleDistance", parameter);
                equipment.planPath(environmentData);
                
                return true; // 事件已处理
            }
        }
        
        // 如果无法处理，传递给下一个处理器
        if (nextHandler != null) {
            return nextHandler.handleSafetyEvent(equipment, eventType, parameter);
        }
        
        return false; // 没有处理器能够处理此事件
    }
}    